<?php
/**
 * Created by PhpStorm.
 * User: suruixiang
 * Date: 2017/10/11
 * Time: 下午5:11
 */
namespace app\admin\controller;

use think\Request;
use app\admin\model\CoursesClassify;

class Courses extends Common
{
    //课程列表
    public function coLists(Request $request)
    {
        $where = [];
        if ($request->param('classify')) {
            $classify = $request->param('classify', 0, 'intval');
            $where['classify_id'] = $classify;
            $this->assign('classify', $classify);
        }
        //是否有收益
        if ($request->param('has_earning')) {
            $has_earning = $request->param('has_earning', 0, 'intval');
            $where['cps_generalize_earning'] = $has_earning == 2 ? 0.00 : ['gt', 0.00];
            $this->assign('has_earning', $has_earning);
        }
        //是否默认banner
        if ($request->param('is_default_banner')) {
            $is_default_banner = $request->param('is_default_banner', 0, 'intval');
            $is_default_banner_where = $is_default_banner == 2 ? 0 : $is_default_banner;
            $where['cps_is_default_banner'] = $is_default_banner_where;
            $this->assign('is_default_banner', $is_default_banner);
        }
        //是否有推文
        if ($request->param('has_generalize')) {
            $has_generalize = $request->param('has_generalize', 0, 'intval');
            $where['cps_generalize_link'] = $has_generalize == 2 ? ['exp', 'is null'] : ['exp', 'is not null'];
            $this->assign('has_generalize', $has_generalize);
        }
        if ($request->param('courses_title')) {
            $courses_title = $request->param('courses_title', '', 'trim');
            $where['title'] = ['like', "%{$courses_title}%"];
            $this->assign('courses_title', $courses_title);
        }
        $coursesModel = new \app\admin\model\Courses();
        $categoryModel = new \app\admin\model\Category();
        $data = $coursesModel->getAllLists($where);
        $classifys = $categoryModel->getAllLists();
        $this->assign('classifys', $classifys);
        $this->assign('lists', $data['lists']);
        $this->assign('page', $data['page']);
        return $this->fetch();
    }
    //编辑课程
    public function coEdit(Request $request)
    {
        $coursesModel = new \app\admin\model\Courses();
        $id = $request->param('id', 0, 'intval');
        if ($request->isPost()) {
            $data['cps_generalize_link'] = $request->post('cps_generalize_link', '', 'trim');
            $data['cps_generalize_earning'] = $request->post('cps_generalize_earning', 0, 'intval');
            if ($request->post('bannel')) {
                $data['cps_banner'] = $request->post('bannel', '', 'trim');
            }
            if ($coursesModel->updateData(['id'=>$id], $data)) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        if (!$id) {
            $this->error('参数错误');
        }
        $this->assign('info', $coursesModel->getInfoById($id));
        return $this->fetch();
    }
    //删除课程
    public function coDelete(Request $request)
    {
        if ($request->isPost()) {
            $id = $request->post('id', 0, 'intval');
            if (!$id) {
                $this->error('参数错误');
            }
            $coursesModel = new \app\admin\model\Courses();
            if ($coursesModel->updateData([
                'id' => $id,
            ], [
                'cps_is_del' => 1
            ])) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
}